<template>
	<div>
		<el-form :inline="true">
			<el-form-item label="起止日期" prop="startandend" label-width="80px">
				<el-date-picker v-model="startandend" type="daterange" align="right" unlink-panels range-separator="至" start-placeholder="开始日期" end-placeholder="结束日期" :picker-options="pickerOptions2">
				</el-date-picker>
			</el-form-item>
			<el-form-item>
				<div style="float: left;">
					<el-button type="primary" @click="showdata">查询</el-button>
					<el-button type="primary" @click="importbutton" >导入</el-button>
					<el-button type="primary" @click="buildVisible = true" >生成</el-button>
				</div>
			</el-form-item>
		</el-form>
		<v-table :chart-data=dateData :items-data=itemsDate v-on:onEdit="showEdit" v-on:selectionChange="handleSelectionChange"></v-table>
	
		<!--新增界面-->
		<el-dialog :title="text" :visible.sync="addFormVisible" :close-on-click-modal="false" @close="resetForm('addForm')" style="padding-bottom:0;">
			<el-form :model="addForm" label-width="80px" ref="addForm"
			style="height:440px;">
				<el-form-item label="日期编号" prop="dkey">
					<el-input v-model="addForm.dkey" :disabled="true" auto-complete="off"></el-input>
				</el-form-item>
				<!-- <el-form-item label="时间" prop="keydate">
					<el-input v-model="addForm.keydate" auto-complete="off"></el-input>
				</el-form-item> -->
				<el-form-item label="日期类型" prop="daytype">
					<el-input v-model="addForm.daytype" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="国定假日" prop="holidaytype">
					<el-input v-model="addForm.holidaytype" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="其他假日" prop="otherholidaytype">
					<el-input v-model="addForm.otherholidaytype" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="比较日期" prop="comparisondate">
					<el-input v-model="addForm.comparisondate" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="系数" prop="coefficient">
					<el-input v-model="addForm.coefficient" auto-complete="off"></el-input>
				</el-form-item>
				<el-form-item label="节气" prop="solarterms">
					<el-input v-model="addForm.solarterms" auto-complete="off"></el-input>
				</el-form-item>
			</el-form>
			<br />
			<div slot="footer" class="dialog-footer" style="text-align: left; margin-left:30px;margin-left:80px;">
				<el-button type="primary" @click.native="addSubmit">提交</el-button>
				<el-button @click="reset()">重置</el-button>
			</div>
		</el-dialog>
		
		<el-dialog title="生成" :visible.sync="buildVisible" :close-on-click-modal="false" style="padding-bottom:0;">
			<el-form>
				<el-form-item label="年份" prop="year" label-width="50px">
					<el-date-picker  v-model="inputyear" type="year" value-format="yyyy" placeholder="选择年"></el-date-picker>
				</el-form-item>
			</el-form>
			<br />
			<div slot="footer" class="dialog-footer" style="text-align: left; margin-left:30px;margin-left:80px;">
				<el-button type="primary" @click.native="buildbutton">生成</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import { query, apiparas, execute } from '../../api/api';
	import { Cookie } from '../../js/common';
	import vTable from '../charts/table.vue';
	export default {
		components:{
            vTable
      	},
		data() {
			return {
				startandend:[],
				source:'',
				text:'',
				inputyear:'',
				dateData:[],
				itemsDate:[
					{prop:'dkey',label:'假日编号'},
					{prop:'keydate',label:'时间'},
					{prop:'daytype',label:'日期类型'},
					{prop:'holidaytype',label:'国定假日'},
					{prop:'otherholidaytype',label:'其他假日'},
					{prop:'comparisondate',label:'比较日期'},
					{prop:'coefficient',label:'系数'},
					{prop:'solarterms',label:'节气'}
				],
				addFormVisible:false,
				buildVisible:false,
				addForm:{
					dkey:'',
					keydate:'',
					daytype:'',
					holidaytype:'',
					otherholidaytype:'',
					comparisondate:'',
					coefficient:'',
					solarterms:''
				},
				pickerOptions2: {
					shortcuts: [{
						text: '最近一周',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 7);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近一个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 30);
							picker.$emit('pick', [start, end]);
						}
					}, {
						text: '最近三个月',
						onClick(picker) {
							const end = new Date();
							const start = new Date();
							start.setTime(start.getTime() - 3600 * 1000 * 24 * 90);
							picker.$emit('pick', [start, end]);
						}
					}]
				},
			}
		},
		mounted: function() {
			//生命周期
		},
		computed: {
			//计算属性
		},
		methods: { //函数
			reset:function(){//重置
				this.addForm.dkey = this.selectRow.dkey;
				this.addForm.daytype = this.selectRow.daytype;
				this.addForm.holidaytype = this.selectRow.holidaytype;
				this.addForm.otherholidaytype = this.selectRow.otherholidaytype;
				this.addForm.comparisondate = this.selectRow.comparisondate;
				this.addForm.coefficient = this.selectRow.coefficient;
				this.addForm.solarterms = this.selectRow.solarterms;
			},
			resetForm:function(formName){//重置
				this.$refs[formName].resetFields();
			},
			showdata: function() { //查询数据
				let starttime = '';
				let endtime = '';
				if(this.startandend == null || this.startandend.length == 0) {
					this.startandend = [];
					this.$notify.error({title: '错误',message:"选择时间有误！！请重新选择" ,type:'error'});
					return ;
				} else {
					starttime = this.formatDate(this.startandend[0]);
					endtime = this.formatDate(this.startandend[1]);
				}
				this.source = "company.showdata(获取公司信息)";	//调用cmd来源
				//显示数据
				let jsonparas = [
					{'name': 'sdate','value': starttime}, 
					{'name': 'edate','value': endtime}, 
				];
				let _paras = apiparas.getParas('default', 'sys|get_date', jsonparas, this.source);
				query(_paras).then(response => {
					if(response.errid == 0){
						let json_data = response.result;
						this.dateData = json_data;
					}
					else
					{
						this.$notify.error({title: '错误',message:response.errmsg ,type:'error'});
					}
				});
				
			},
			handleSelectionChange(val){
				this.multipleTable = val;
			},
			//编辑
			showEdit:function(index,row){
				this.addFormVisible = true;
				this.text = '修改';	
				this.selectRow = row;
				this.addForm.dkey = row.dkey;
				this.addForm.daytype = row.daytype;
				this.addForm.holidaytype = row.holidaytype;
				this.addForm.otherholidaytype = row.otherholidaytype;
				this.addForm.comparisondate = row.comparisondate;
				this.addForm.coefficient = row.coefficient;
				this.addForm.solarterms = row.solarterms;
			},

			addSubmit:function(){
				this.source = "sys.addSubmit(修改日期)";	//调用cmd来源
				//增加查询条件
				let jsonparas = [
					{'name': 'dkey','value': this.addForm.dkey},
					{'name': 'daytype','value':  this.addForm.daytype},
					{'name': 'holidaytype','value':this.addForm.holidaytype},
					{'name': 'otherholidaytype','value': this.addForm.otherholidaytype},
					{'name': 'comparisondate','value': this.addForm.comparisondate},
					{'name': 'coefficient','value': this.addForm.coefficient},
					{'name': 'solarterms','value': this.addForm.solarterms},
				]
				//this.$notify({title: '信息提示',message: '正在加载数据，请稍后！',type: 'info'});
				//查询汇总
				let _paras = apiparas.getParas('default','sys|update_date', jsonparas,this.source);
				execute(_paras).then(response => {
					if(response.errid == 0){
						this.addFormVisible = false;
						this.$notify({title:'成功信息',message:'修改完成',type:'success'});
						this.showdata();
					}
					else
					{
						this.$notify.error({title: '错误',message:response.errmsg ,type:'error'});
					}
					
				});	
				
			},
			formatDate: function(date) {
				if(typeof(date) == "string") {
					return date
				}
				let y = date.getFullYear();
				let m = date.getMonth() + 1;
				m = m < 10 ? '0' + m : m;
				let d = date.getDate();
				d = d < 10 ? ('0' + d) : d;
				return y + '-' + m + '-' + d;
			},
			importbutton:function(){
				
			},
			buildbutton:function(){
				this.source = "sys.buildbutton(修改日期)";	//调用cmd来源
				//增加查询条件
				let jsonparas = [
					{'name': 'year','value': this.inputyear}					
				]
				//this.$notify({title: '信息提示',message: '正在加载数据，请稍后！',type: 'info'});
				//查询汇总
				let _paras = apiparas.getParas('default','sys|build_date', jsonparas,this.source);
				execute(_paras).then(response => {
					if(response.errid == 0){
						this.$notify({title:'成功信息',message:'生成成功',type:'success'});
						this.showdata();
					}
					else
					{
						this.$notify.error({title: '错误',message:response.errmsg ,type:'error'});
					}
					this.buildVisible = false;
				});	
				
			},
		},
		created: function() {
			//获取当前日期和当月第一天的日期
			let _date = new Date();
			let year = _date.getFullYear();
			let month = _date.getMonth() + 1;
			month =month<10?'0'+month:month;
			this.startandend[0] = year + '-' + month + '-' + '01';
			this.startandend[1] = this.formatDate(_date);
			this.showdata();
		}
	}
</script>

<style>

</style>